package com.hk.huakangcompany.controller;

import com.hk.huakangcompany.commons.security.CommonResult;
import com.hk.huakangcompany.model.Admin;
import com.hk.huakangcompany.model.WxUserRoleRelation;
import com.hk.huakangcompany.model.vo.Result;
import com.hk.huakangcompany.service.WxUserRoleRelationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/26 10:40
 * @Statement: 用户角色中间表 前端控制器
 */
@RestController
@RequestMapping("/admin-role")
public class WxUserRoleRelationController {

    @Autowired
    WxUserRoleRelationService userRoleRelationService;

    @GetMapping("/findByAdminId/{id}")
    public Result findByAdminId(@PathVariable Integer id) {
        List<WxUserRoleRelation> userRoleRelationList = userRoleRelationService.findByAdminId(id);
        List<Integer> roleIds = userRoleRelationList.stream().map(WxUserRoleRelation::getRoleId).collect(Collectors.toList());
        Admin admin = Admin.builder().id(id)
                .rids(roleIds).build();
        if (userRoleRelationList.isEmpty()) {
            Result result = CommonResult.nullResult(admin);
            return result;
        }
        Result result = CommonResult.successResult(admin);
        return result;
//        return null;
    }

    @ApiOperation(("管理员添加角色"))
    @PreAuthorize("hasAuthority('admin-role:create')")
    @PostMapping("/save")
    public Result save(@RequestBody WxUserRoleRelation userRoleRelation) {
//        System.out.println("roles==="+userRoleRelation);
        if (userRoleRelation == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        userRoleRelationService.save(userRoleRelation);
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }


}
